package stack;

import java.util.Iterator;

public class StackOfArray<Item> implements Iterable<Item>{

    Item[] a = (Item[])new Object[1];

    int count;

    public StackOfArray(){}

    public void push(Item item){
        if(count == a.length){
            resize(a.length * 2);
        }
        a[count ++ ] = item;
    }

    public Item pop(){
        Item item = a[--count];
        a[count] = null;
        if(count > 0 && count == a.length / 4){
            resize(a.length/2);
        }
        return item;
    }

    public void resize(int length){
        Item[] temp = (Item[])new Object[length];
        for(int i = 0; i< a.length; i++){
            temp[i] = a[i];
        }
        a = temp;
    }

    public Iterator<Item> iterator() {
        return null;
    }
}
